<?php



require_once(dirname(__FILE__).'/../../common/log.php');
require_once(dirname(__FILE__).'/../../common/defines.php');
require_once(dirname(__FILE__).'/../../common/return.php');
require_once(dirname(__FILE__).'/../../database/mysql.class.php');

require_once(dirname(__FILE__).'/../common.php');


$body  = file_get_contents('php://input');
logd("recv: ".$body);
$data  = json_decode($body, true);

if ((!isset($data["from"])) || (!isset($data['type'])) || (!isset($data['receipt'])) || (!isset($data['offline'])))
{
    failure(ERR_PARAM);
}

if ((!isset($data['to'])) || (!is_array($data['to'])) || empty($data['to']))
{
	logd("to is error");

	failure(ERR_PARAM);
}

if ((!isset($data['msg'])) || (!is_array($data['msg'])) || empty($data['msg']))
{
	logd("msg is error");

	failure(ERR_PARAM);
}

//通话信令需要修改客服id
if (($data['type'] == MSG_TYPE_CALLOUT ) || ($data['type'] == MSG_TYPE_CONFIRM) || ($data['type'] == MSG_TYPE_HANGUP))
{
    //连接数据库
    $mysql  = new mysqldb();
    $ret    = $mysql->open_db();
    if ($ret === FAILURE)
    {
        exit();
    }

    $ret  = $mysql->find('ykj_app_user', 'account_type', 'id='.$data['from']);
    if ($ret['account_type'] == ACCOUNT_TYPE_KEFU)
    {
        $data['from']  = USERID_SERVER;
    }
}

send_kmsg_message($data['from'], $data['to'], $data['type'], $data['receipt'], $data['msg'], $data['offline']);

success();

